<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * CodeIgniter
 *
 * Funçoes criadas por Michael Frandin da Silva.
 *
 */

// ------------------------------------------------------------------------

/**
 * Add arquivo de tradução conforme controller
 */
if ( ! function_exists('get_translation'))
{
	function get_translation($arq = '', $lang = '') {
		include APPPATH.'language/'.cur_lang($lang).'/'.$arq.'.inc';
	}
}
/**
 * Add arquivo de tradução conforme controller
 */
if ( ! function_exists('get_user_login'))
{
	function get_user_login($session) {
		return (isset($session->userdata['user_data']['user_login']))?$session->userdata['user_data']['user_login']:'';
	}
}
/**
 * Add arquivo de tradução conforme controller
 */
if ( ! function_exists('get_user_permissions'))
{
	function get_user_permissions($session) {
		return $session->userdata['user_data']['permissions'];
	}
}
/**
 * Gets the current language ISO Code.
 * <p>It can be different from <c>user_lang</c> because it takes into account all the available languages to the application.</p>
 * return Current ISO code.
 */
if ( ! function_exists('gerar_token'))
{
	
	function gerar_token($quantidade = 50)
	{
		$CaracteresAceitos = 'abcdefghijklmnopqrstuvxwyz0123456789'; 
		$max = strlen($CaracteresAceitos)-1;
		$password = "";
		for($i=0; $i < $quantidade; $i++) { 
			$password .= $CaracteresAceitos{mt_rand(0, $max)}; 
		}
		return $password;	
	}
}

/**
 * Função de gerar numeros randomicos
 * @param $unidade
 * @param $dezena
 * @param $constante � uma valor que pode ser atrobuido como bonus
 * @retuen float
 * */
if ( ! function_exists('create_float'))
{
	function create_float($unidade, $dezena, $constante = 0) {
		// @TODO Verificar metodos de cria��o de jogadores, pra teste irei usar esse mesmo
		$unidade += $constante;
		$unidade = ($unidade < 10)?'0'.$unidade:$unidade;
		$dezena = ($dezena < 10)?'0'.$dezena:$dezena;
		
		return $unidade.'.'.$dezena;
	}
}

/**
 * COnverte de moedas para flutuantes
 * */
if ( ! function_exists('to_float'))
{
	function to_float($valor_r)
	{	
		$valor_f1 = str_replace(".","",$valor_r);
		$valor_f = str_replace(",",".",$valor_f1);
		return $valor_f;
	}
}

/**
 * COnverte numeros flutuantes para moeda
 * */
if ( ! function_exists('to_moeda'))
{	
	function to_moeda($valor_f)
	{	
		$valor_r = number_format($valor_f, 2, ',', '.');
		return $valor_r;
	}
}

/**
 * Converte data mysql para br
 * */
if ( ! function_exists('mysql_to_br'))
{	
	function mysql_to_br($data)
	{
		$d = explode("-",$data);
	
		return $d[2]."/".$d[1]."/".$d[0];
	}	
}

/**
 * Converte data br para mysql
 * */
if ( ! function_exists('br_to_mysql'))
{	
	function br_to_mysql($data)
	{
		$d = explode("/",$data);
						
		return $d[2]."-".$d[1]."-".$d[0];
	}
}
/**
 * Gets the current language ISO Code.
 * <p>It can be different from <c>user_lang</c> because it takes into account all the available languages to the application.</p>
 * return Current ISO code.
 */
if ( ! function_exists('cur_lang'))
{
	function cur_lang($l = '') {
		$lang = user_lang($l);
		
		//Checks if the directory for the current user language exists.
		if (!file_exists(APPPATH.'language/'.$lang)) {
			$lang = DEFAULT_LANGUAGE;
		}
		
		return $lang;
	}
}

/**
 * Gets the current date and time.
 * @returns date
 */
if ( ! function_exists('cur_date'))
{
	function cur_date() {
		return date('Y-m-d H:i:s');
	}
}
/**
 * Gets the current date and time.
 * @returns date
 */
if ( ! function_exists('get_userdata'))
{
	function get_userdata($session) {
		return $session->userdata['user_data'];
	}
}

/**
 * Gets the current date and time.
 * @returns date
 */
if ( ! function_exists('url'))
{
	function url($session, $segment = '') {
		$segments = get_url_segments($session);
		$url = '';
		if($segment){
			if (count($segments) > 0){
				$lang = cur_lang($segments[1]);
				$url = site_url($lang.'/'.$segment);
			}
			else {
				$url = site_url(DEFAULT_LANGUAGE.'/');
			}
		}
		else{
			if (count($segments) > 0){
				$lang = cur_lang($segments[1]);
				if(isset($segments[2])){
					$action = '';
					for($i = 2; $i <= count($segments); $i++)
						$action .= $segments[$i].'/';
				} else {
					$action = $segments[1];
				}
				$url = site_url($lang.'/'.$action);
			}
			else {
				$url = site_url(DEFAULT_LANGUAGE.'/');
			}
		}
		return $url;
	}
}

/**
 * Gets the current language ISO Code for the user.
 * @return Current ISO code.
 */
if ( ! function_exists('user_lang'))
{
	function user_lang($lang = '') {
		return ($lang)?$lang:substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
	}
}

/**
 * Gets segments for URL
 * @return array
 * */
if ( ! function_exists('get_url_segments'))
{
	function get_url_segments($session) {
		return $session->CI->uri->segments;
	}
}

/**
 * Copies data from a list of objects to another list based on its properties.
 * @param string $toClassName Name of the class of the array item objects.
 * @param array $from Array from which the objects will be read.
 * @param array $to Array to which the objects will be copied.
 * @return array
 */
if ( ! function_exists('copy_to_array'))
{
	function copy_to_array($toClassName, $from, &$to = null) {
		if ($to == null) $to = array();
		
		$i = 0;
		foreach ($from as $obj) {
			$to[$i] = new $toClassName;
			copy_to($obj, $to[$i++]);
		}
		
		return $to;
	}
}

/**
 * Copies data from one object to another based on its properties.
 * @param object $from Object from which the data will be read.
 * @param object $to Object to which the data will be copied.
 * @return object
 */
if ( ! function_exists('copy_to'))
{
	function copy_to($from, &$to) {
		foreach ($from as $info) {//Usando essa linha porque no codeIgniter sempre retorna a busca em um array
			foreach ($from as $key => $value) {
				if ($to instanceof ArrayAccess && $to->offsetExists($key)) {
					$to->offsetSet($key, $value);
				} else if (property_exists($to, $key)) {
					$to->$key = $value;
				}
			}
		}
		return $to;
	}
}

/* End of file Helper.php */
/* Location: ./application/helpers/Helper.php */